﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using metroCIS.Library;
using Windows.UI.Xaml.Data;

namespace metroCIS.ViewModel
{
    public class LVPlanViewModel : ViewModelBase
    {
        public Dictionary<string, List<LvEntry>> Lessons { get { return _lessons; } set { _lessons = value; OnPropertyChanged("Lessons"); } }
        
        private readonly CisManager _manager = CisManager.Create();
        private CollectionViewSource _viewSource = new CollectionViewSource();
        private Dictionary<string, List<LvEntry>> _lessons;

        public LVPlanViewModel()
        {
            Refresh();
        }

        private async void Refresh()
        {
            var lessions = await _manager.GetLVPlan();
            var groupedLessions = lessions.GroupBy(k => GroupDate(k.From.Date)).ToDictionary(k => k.Key, v => v.ToList());
            Lessons = groupedLessions;
        }

        private string GroupDate(DateTime dateTime)
        {
            if (dateTime == DateTime.Today)
                return "Heute";
            if (dateTime == DateTime.Today.AddDays(1))
                return "Morgen";

            return dateTime.ToString("M");
        }
    }
}
